package org.jadapter.tests.spring;

import static org.junit.Assert.assertTrue;

import org.jadapter.examples.foobar.Bar;
import org.jadapter.examples.foobar.BarCsv;
import org.jadapter.examples.foobar.Foo;
import org.jadapter.examples.foobar.FooBar;
import org.jadapter.examples.foobar.FooBean;
import org.jadapter.tests.examples.Csv;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestTransparendUsage {
	
	
	@Test 
	public void usingJAdapterShouldBeTransparentInClientCode() {
	
		// first we need to get the bean from the context
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "org/jadapter/tests/spring/transparent-context.xml");

        Foo foo = (Foo)ctx.getBean("foo");
        
        // ok now write some client code..
               
        // mmm, I wish I had a bar of...
        Bar bar = (Bar)foo;
        
        // I am sure that bar is more than foo
        assertTrue(bar.bar() > foo.foo());
        
        // I wonder if I can serialize my foo for later...
        Csv csv = (Csv)foo;
        
        // is foo The Answer to Life, the Universe, and Everything???
        assertTrue("42".equals(csv.csv()));
        	
		// Foo is 41, it gets transformed to Bar, which is one more that Foo => 42, 
        // then the Bar is transformed to Csv, which gives us the String.

        // The composition behind the scenes looks like this:
        Csv answer = new BarCsv(new FooBar(new FooBean(41)));
        assertTrue("42".equals(answer.csv()));

	}

}
